package com.gxuwz.business.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.entity.Medicine;
import com.gxuwz.business.service.IMedicineService;
import com.gxuwz.util.Pager;
import com.gxuwz.util.UidUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONObject;

public class MedicineAction extends BaseAction implements Preparable,ModelDriven<Medicine>{

	private static final long serialVersionUID = 7978289945844975496L;
	
	private static final String MEDICINE_LIST = "/WEB-INF/page/medicine/medicine_list.jsp";
	private static final String MEDICINE_ADD = "/WEB-INF/page/medicine/medicine_add.jsp";
	private static final String MEDICINE_UPDATE = "/WEB-INF/page/medicine/medicine_update.jsp";
	private static final String MEDICINE_CHOICE = "/WEB-INF/page/medicine/medicine_choice.jsp";
	
	private Medicine medicine;
	
	@Autowired
	private IMedicineService medicineService;
	
	private Pager<Medicine> medicineList;//分页数据
	private List<Medicine> medicineLists;
	
	/**
	 * 打开药品列表界面
	 * @return
	 */
	public String medicineList(){
		forwardView = MEDICINE_LIST;
		return SUCCESS;
	}
	
	/**
	 * 分页查询
	 */
	public String getByPager(){
		medicineList = medicineService.getByPager(medicine, page, limit);
		JsonData = JSONObject.fromObject(medicineList);
		return "JsonObject";
	}
	
	/**
	 * 伪删除挂号信息
	 */
	@SuppressWarnings("deprecation")
	public String delete(){
		isSucceed = medicineService.delete(medicine);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
		
	}
	
	/**
	 * 打开添加挂号界面
	 */
	public String openAdd(){
		forwardView = MEDICINE_ADD;
		return SUCCESS;
	}
	
	/**
	 * 添加药品信息
	 */
	@SuppressWarnings("deprecation")
	public String add(){
		//随机生成挂号编号
		medicine.setMedId(UidUtils.UIDD());
		medicine.setStatus(0);
		String medTime = medicine.getMedTime();
		String medTimes = medTime+"个月";
		medicine.setMedTime(medTimes);
		isSucceed = medicineService.add(medicine);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
	}
	
	
	/**
	 * 检测药品是否已存在
	 */
	@SuppressWarnings("deprecation")
	public String checkMedName(){
		isSucceed =  medicineService.checkMedName(medicine);
		jsonString = jsonToString(isSucceed);
		System.out.println(jsonString);
		return "JsonString";
	}
	
	/**
	 * 打开更新界面
	 */
	public String openUpdate(){
		System.out.println(medicine.getMedId());
		medicine = medicineService.findById(medicine);
		System.out.println(medicine);
		forwardView = MEDICINE_UPDATE;
		return SUCCESS;
	}
	
	/**
	 * 更新信息
	 */
	@SuppressWarnings("deprecation")
	public String update(){
		String medTime = medicine.getMedTime();
		String medTimes = medTime+"个月";
		medicine.setMedTime(medTimes);
		isSucceed = medicineService.update(medicine);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
		
	}
	
	/**
	 * 打开药品选择弹窗
	 */
	public String openChoice(){
		forwardView = MEDICINE_CHOICE;
		return SUCCESS;
	}
	
	/**
	 * 获取全部
	 */
	@SuppressWarnings("unused")
	public String getAll(){
		//一次性将所有的药品赛到分页数据里
		medicineLists = medicineService.getAll(Medicine.class);
		Pager<Medicine> pager = new Pager<Medicine>(0, "", medicineLists.size() ,medicineLists);
		JsonData = JSONObject.fromObject(pager);
		return "JsonObject";
	}
	
	
	@Override
	public Medicine getModel() {
		return medicine;
	}

	@Override
	public void prepare() throws Exception {
		if(null == medicine){
			medicine = new Medicine();
		}
	}

	public Medicine getMedicine() {
		return medicine;
	}

	public List<Medicine> getMedicineLists() {
		return medicineLists;
	}

	public void setMedicineLists(List<Medicine> medicineLists) {
		this.medicineLists = medicineLists;
	}
	
	
	
	
}
